﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.标签打印.Service;
using Seagull.BarTender.Print;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.销售台账.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.service;
using System.IO;
using System.Text.RegularExpressions;
using DXApplicationTangche.Utils;
using mendian;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form水洗标 : DevExpress.XtraEditors.XtraForm
    {
        private List<washDTO> washDTOs { get; set; } = new List<washDTO>();
        private String standard;
        private OrderCommitDTO返回 orderCommitDTO = new OrderCommitDTO返回();
        public Form水洗标()
        {
            InitializeComponent();
        }
        public Form水洗标(String barcode)
        {
            InitializeComponent();
            this.batchNo.Enabled = false;
            washDTO washDTO = 标签打印Service.getWarterMarkPrintLogByBarcode(barcode);
            try { washDTO.styleSizeCd = washDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { };
            this.washDTOs.Add(washDTO);
            this.gridControl水洗标.DataSource = this.washDTOs;
        }
        private void batchNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.batchNo.Text))
                {
                    MessageBox.Show("请扫描批号");
                    return;
                }
                try
                {
                    //判断裁剪是否已扫
                    DXApplicationTangche.UC.下单MTO.Service.PlainMessageResult plainMessageResult =
                        生产批生产Service.judgeClippingByBatchNo(this.batchNo.Text.Trim());
                    if (ConstPlainMessageResult.OK.Equals(plainMessageResult.errorCode))
                    {
                        //赋值，给gridControl数据源绑定
                        this.washDTOs = new 标签打印Service(this.batchNo.Text.Trim()).washDTOs;
                    }
                    else
                    {
                        //MessageBoxButtons messBtn = MessageBoxButtons.OKCancel;
                        //DialogResult dr = MessageBox.Show("审版未扫码，是否继续打印洗涤？", "提示:确认", messBtn);
                        //if (dr == DialogResult.OK)
                        //{
                        //    this.washDTOs = new 标签打印Service(this.batchNo.Text.Trim()).washDTOs;
                        //}
                        MessageBox.Show(plainMessageResult.errorMessage);
                        this.washDTOs.Clear();
                    }
                }
                catch
                {
                    MessageBox.Show("无水洗标信息");
                    this.washDTOs.Clear();
                }
                this.gridControl水洗标.DataSource = this.washDTOs;
                this.gridView水洗标.RefreshData();
                this.batchNo.Clear();
            }
        }
        private void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
        {
            //if (!String.IsNullOrWhiteSpace(this.orderCommitDTO.kashiyamaOrderNo))
            //{
            //    if (
            //        销售台账Service.getKashiyamaJyutyuRecordsByProductNo(this.orderCommitDTO.kashiyamaOrderNo).Count > 0
            //        )
            //    {
            //        this.print受托加工水洗();
            //        return;
            //    }
            //}
            try
            {
                //  根据款式查询工信部质量标准
                String styleNameCn = this.washDTOs[this.gridView水洗标.FocusedRowHandle].styleNameCn;
                this.standard = 标签打印Service.getMIITStandardByStyleCode(styleNameCn);
            }
            catch
            {
                this.standard = "FZ/T 73020-2019";
            }
            try
            {
                String printFileName = @"tag\" + "PrintConfig.xml";
                if (!FileUtil.FileExist(printFileName))
                {
                    //将传参的打印机名称保存到配置文件中
                    PrintMachineDTO printMachineDTO传参 = new PrintMachineDTO();
                    new Form打印机配置文件(printMachineDTO传参).ShowDialog();
                    DealXML.ObjectToXMLFile(new PrintMachineDTO("01", printMachineDTO传参.printName), printFileName, Encoding.UTF8);
                }
                PrintMachineDTO printMachineDTO = DealXML.XMLFlieToObject<PrintMachineDTO>(FileUtil.getAbsolutePath(printFileName), Encoding.UTF8);
                //判断成份是否为空，为空不能打印
                if (String.IsNullOrEmpty(this.washDTOs[this.gridView水洗标.FocusedRowHandle].composition))
                {
                    MessageBox.Show("面料成份为空，不能打印水洗");
                    return;
                }
                String rtn = 标签打印Service.print水洗(this.washDTOs[this.gridView水洗标.FocusedRowHandle], true, printMachineDTO.printName);
                //MessageBox.Show("cnm"+rtn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //标签打印Service.print套号水洗(this.washDTOs[this.gridView水洗标.FocusedRowHandle],true);
        }


        private void print受托加工水洗()
        {
            Engine btEngine = new Engine();
            btEngine.Start();
            //LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\Users\\lenovo\\Desktop\\坚山水洗标.btw");
            LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\坚山水洗标.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "TEC B-462-R";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 

            //赋值的时候因为现在是List 不是单个对象，要把这一行的数赋值给gridView，所以用e.RowHandle
            try
            {
                btFormat.SubStrings["orderDate"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].batchNo;
            }
            catch { }
            try
            {
                btFormat.SubStrings["seisanSosikiCd"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].orderId;
            }
            catch { }
            try
            {
                btFormat.SubStrings["productNo"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].orderNo;
            }
            catch { }
            try
            {
                btFormat.SubStrings["custKana"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].customerName;
            }
            catch { }
            try
            {
                btFormat.SubStrings["item"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].materialCode;
            }
            catch { }
            try
            {
                btFormat.SubStrings["brand"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].barcode;
            }
            catch { }
            try
            {
                btFormat.SubStrings["period"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].styleNameCn;
            }
            catch { }
            try
            {
                btFormat.SubStrings["itemNumber"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].styleSizeCd;
            }
            catch { }
            try
            {
                btFormat.SubStrings["composition"].Value = this.washDTOs[this.gridView水洗标.FocusedRowHandle].composition;
            }
            catch { }
            
            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Seagull.BarTender.Print.Result nResult1 = btFormat.Print("水洗标" + "", waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }

    }
}